package com.feihao.zhou.demo1;

import java.util.ArrayList;

public class CollectionRemoveIf {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("bb");
        list.add("bb");
        list.add("cc");
        list.add("aa");
        list.add("dd");

        System.out.println(list);

        //使用list的removeIf(Predicate );
        /*list.removeIf(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.equals("bb");
            }
        });*/

        removeIfCopy(list, new Predicate111<String>() {
            @Override
            public boolean panDuan(String e) {
                return e.equals("bb");
            }
        });


        System.out.println(list);

    }

    //自定义removeIf方法：
    public static void removeIfCopy(ArrayList<String> list,Predicate111<String> pre){//传递集合；再传递一个接口进行判断。
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (pre.panDuan(s)){
                list.remove(s);
                i--;
            }
        };
    }

}

 interface Predicate111<T>{
    abstract boolean panDuan(T e);
 }
